layui.use(['element','layer', 'dtree'],function () {
    var layer = layui.layer,dtree = layui.dtree,$ = layui.$;
    var base = $("#baseId").context.baseURI;
    var categoryTree = dtree.render({
        elem: '#goodsCategory',
        url: base + '/goods/getCategoryTree.html',
        method: 'POST',
        //checkbar: true, //是否开启复选框
        //checkbarType: "all",
        initLevel: 3,
        icon: 2,
        dataFormat: 'list',
        toolbar: true,
        toolbarScroll: '#toolbarDiv', //划重点，必须配置，属性含义请参考文档
        toolbarLoad: 'node',  // 表示工具栏加载的节点，  可选的值还有：noleaf（非最后一级）,leaf（最后一级节点）
        toolbarStyle: {title:"分类", area:["30%","40%"]},
        dataStyle: 'layuiStyle',
        dot: false,  // 隐藏小圆点
        response:{message:"errMsg",code:200,statusName : "code"},
        toolbarFun: {
            addTreeNode: function(treeNode){
                treeNode["nodeId"] = '-1';
                var addNodeId = updateCategoryNode(treeNode,1);
                treeNode["nodeId"] = addNodeId;
                categoryTree.changeTreeNodeAdd(addNodeId); // 配套使用
            },
            editTreeNode: function(treeNode){
                categoryTree.changeTreeNodeEdit(updateCategoryNode(treeNode,2)); // 配套使用
            },
            delTreeNode: function(treeNode){
                categoryTree.changeTreeNodeDel(updateCategoryNode(treeNode,3)); // 配套使用
            }
        }
    });

    function updateCategoryNode(treeNode,type,callback) {
        var isSuccess = true;
        var addNode;
        $.ajax({
            type: "POST",
            url: base + "/goods/updateCategoryNode",
            data: {"parentId": parseInt(treeNode.parentId),"title":treeNode.context,"id":parseInt(treeNode.nodeId),"type":type},
            dataType: "json",
            async: false,
            success: function(resp){
                if (resp.code != 200) {
                    layer.msg(resp.errMsg,{icon: 5,anim: 6});
                    isSuccess = false;
                } else {
                    addNode = resp.data +'';
                }
            }
        });
        if (type < 2) {
            return addNode;
        } else {
            return isSuccess;
        }
    };
})